package com.zhuzi.controller;

import com.zhuzi.utils.ClientUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;

/**
 * @author zhuzi
 * 2022/8/2 17:06
 */
@RestController
@Component
@EnableScheduling
public class TestController {
    //在一个方法中连续调用多次WebService接口，每次调用前需要重置上下文。
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    //@Scheduled(cron = "*/10 * * * * ?")
    public String getMessage() {
        Thread.currentThread().setContextClassLoader(classLoader);//在获取连接之前 还原上下文
        System.out.println("======开始调用webservice接口=====");
        String url = "http://127.0.0.1:8082/Service/CatService?wsdl";
        String methodName = "message";
        System.out.println("Calling" + url);
        String result = "";
        try {
            result = ClientUtil.callWebSV(url, methodName, "name");
        } catch (Exception e) {
            System.err.println("接口调用失败！！！！");
            e.printStackTrace();
            return "失败";
        }
        System.out.println("===Finished!===恭喜你啊！！！CatService接口调用成功！！！===获得的数据是：" + result);
        return "Finished!";
    }

    @Scheduled(cron = "*/5 * * * * ?")
    public String getMessage2() {
        Thread.currentThread().setContextClassLoader(classLoader);//在获取连接之前 还原上下文
        System.out.println("======开始调用webservice接口=====");
        String url = "http://localhost:8082/Service/testService?wsdl";
        String methodName = "getFlag";
        System.out.println("Calling" + url);
        String result = "";
        try {
            result = ClientUtil.callWebSV(url, methodName, "username", true);
        } catch (Exception e) {
            System.err.println("接口调用失败！！！！");
            return "失败";
        }
        System.out.println("===Finished!===恭喜你啊！！！TestService接口调用成功！！！===获得的数据是：" + result);
        return "Finished!";
    }

    public String getMessage3() {
        Thread.currentThread().setContextClassLoader(classLoader);//在获取连接之前 还原上下文
        System.out.println("======开始调用webservice接口=====");
        String url = "http://localhost:8082/webService/TestService?wsdl";
        String methodName = "sendMessage";
        System.out.println("Calling" + url);
        String result = "";
        try {
            result = ClientUtil.callWebSV(url, methodName, "username");
        } catch (Exception e) {
            System.err.println("接口调用失败！！！！");
            return "失败";
        }
        System.out.println("===Finished!===恭喜你啊！！！TestService接口调用成功！！！===获得的数据是：" + result);
        return "Finished!";
    }

    public static void main(String[] args) throws IOException {
        String context = FileUtils.readFileToString(new File("/Users/zhuzi/workspace/a-wk/河南/接口文档/报文/测试接口-82.xml"));
        String url = "http://127.0.0.1:8082/Service/parseXml?wsdl";
        String methodName = "parseSecondTierXml";
        methodName = "parseXml";
        System.out.println("Calling" + url);
        String result = "";
        try {
            result = ClientUtil.callWebSV(url, methodName, context, "0x82");
        } catch (Exception e) {
            System.err.println("接口调用失败！！！！");
            e.printStackTrace();

        }
        System.out.println("===Finished!===恭喜你啊！！！CatService接口调用成功！！！===获得的数据是：" + result);
    }
}
